package com.doyens.gmall.mq.receiver;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

@Component
@Configuration
public class ConfirmReceiver {
    //bindings 设置监听哪个交换机，哪个routingKey 哪个队列
    @RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = "queue.confirm",autoDelete = "false"),
            key = "routing.confirm",
            exchange = @Exchange(value = "exchange.confirm",autoDelete = "true")
    ))
    public void process(Message message, Channel channel) throws IOException {
        String str = new String(message.getBody(),"UTF-8");
        System.out.println("收到的消息:"+str);
        //告诉rabbitMQ，收到了这条消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
